import http from '../../utils/httpRequest.js'

let testAvatar = "https://thirdwx.qlogo.cn/mmopen/vi_32/Y44BhqAUMialxdAQU19a9m3ciapZGUZL7BRXeSE4gKdF8Qo99wbhaAicUfUw62p2WW6JHFojQiaXuE21iaNWCSWONog/132"
let userListTest = [
    {
        id: 1,
        shopId: 1,
        phone: "17058179046",
        username: "u1",
        roleIdList: [1],
        roleNames: "超级管理员",
        avatar: testAvatar,
        status: 1
    },
    {
        id: 2,
        shopId: 1,
        phone: "17058179047",
        username: "u2",
        roleIdList: [2, 3],
        roleNames: "后台管理员,游客",
        avatar: testAvatar,
        status: 1
    },
    {
        id: 3,
        shopId: 1,
        phone: "17058179049",
        username: "user3",
        roleIdList: [3],
        roleNames: "游客",
        avatar: testAvatar,
        status: 1
    },
]

// 管理员列表
export function getSysUserList(params) {
    http({
        ...params,
        path: "/system/sysUser/list",
        testData: userListTest
    })
}

// 添加管理员
export function addSysUser(params) {
    http({
        ...params,
        method: "post",
        path: "/system/sysUser",
        testData: function () {
            params.data.id = Math.random()
            params.data.roleIdList = []
            params.data.status = 1
            userListTest.push(params.data)
            return params.username + "xxx"  // 返回系统生成的密码
        },
    })
}

// 删除管理员
export function deleteSysUser(params) {
    http({
        ...params,
        method: "delete",
        path: "/system/sysUser/" + params.sysUserId,
        testData: function () {
            for (let i = userListTest.length - 1; i >= 0; i--)
                if (userListTest[i].id === params.sysUserId)
                    userListTest.splice(i, 1)
            return true
        },
    })
}

// 更新管理员
export function updateSysUser(params) {
    http({
        ...params,
        method: "put",
        path: "/system/sysUser/" + params.data.id,
        testData: function () {
            for (let i = 0; i < userListTest.length; i++)
                if (userListTest[i].id === params.data.id) {
                    userListTest[i] = params.data
                    userListTest[i].roleNames = "角色名列表：" + userListTest[i].roleIdList
                }
            return true
        },
    })
}
